import React, { useCallback } from "react";
import { Button, Modal, message,Radio } from "antd";
import { Service } from "@/lib";
import { IWithLoadingApi } from "@/lib/AppView";
import { CONST_ACTIVITY_APPLY_TASK_USERS } from "@/config/constant";


const ApproverUsers = ({ data, onChange }: { data: any[], onChange: (id: string) => void }) => {
    return (<section>
        <Radio.Group onChange={({ target }) => onChange(target.value)}>
        {
            data.map((item) => {
                return (<Radio key={item.id} value={item.id}>{item.userName}{ item.company ? `(${item.company})`: ""}</Radio>)
            })
        }
        </Radio.Group>
        </section>)
};
export const useBeforeApproveActivityApply = () => {
    const serviceApi = Service.useService();
    return useCallback((data: any, loadingApi: IWithLoadingApi) => {
        return new Promise((resolve, reject) => {
            const applyTaskUsers = sessionStorage.getItem(CONST_ACTIVITY_APPLY_TASK_USERS);
            const taskUsers = applyTaskUsers && applyTaskUsers.length > 0 ? JSON.parse(applyTaskUsers) : [];
            const openAuthRight = (userList: any[]) => {
                let selectedUserId: string|null = null;
                return new Promise((_resolve, _reject) => {
                    const configModal = Modal.confirm({
                        title: "选派单位",
                        content: (<ApproverUsers data={userList} onChange={(id: string) => {
                            selectedUserId = id;
                            configModal.update({
                                "okButtonProps": {}
                            })
                        }}/>),
                        okButtonProps: {
                            disabled: true
                        },
                        onOk: () => {
                            if(!selectedUserId) {
                                message.error("请选择单位");
                                return;
                            }
                            configModal.update({
                                "okButtonProps": {
                                    "loading": true
                                },
                                "open": true
                            });
                            serviceApi.send({
                                endPoint: "activity.applyAuthUser",
                                data: {
                                    applyId: data.id,
                                    userId: selectedUserId
                                }
                            }).then((resp: any) => {
                                const statusCode = resp.statusCode;
                                if(/^200$/.test(statusCode.toString())) {
                                    configModal.update({
                                        open: false
                                    });
                                    configModal.destroy();
                                    _resolve({
                                        status: 2
                                    });
                                } else {
                                    message.error(resp.message || "更新受理单位失败");
                                    _reject(message);
                                }
                            }).catch((error) => {
                                message.error(error.message || "指定受理单位失败。");
                                configModal.update({
                                    "okButtonProps": {
                                        "loading": false
                                    }
                                });
                                _reject(error);
                            });
                        },
                        onCancel: () => {
                            loadingApi.hide();
                            reject();
                        }
                    });
                });
            }
            if(taskUsers.length > 0 ) {
                openAuthRight(taskUsers).then(resolve).catch(reject);
            } else {
                loadingApi.show();
                serviceApi.send({
                    endPoint: "activity.applyDealwithUsers"
                }).then(({ data }: any) => {
                    loadingApi.hide();
                    sessionStorage.setItem(CONST_ACTIVITY_APPLY_TASK_USERS, JSON.stringify(data));
                    openAuthRight(data).then(resolve).catch(reject);
                }).catch((err) => {
                    loadingApi.hide();
                    reject(err)
                    message.error(err.message || "获取受理单位信息失败。");
                });
            }
        });
    }, []);
};